package com.gitee.wsl.compose.table

import androidx.compose.runtime.Composable
import com.gitee.wsl.compose.chart.sample.SampleView
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.gitee.wsl.compose.ui.table.model.CheckboxColumnSpec
import com.gitee.wsl.compose.ui.table.model.ColumnSpec
import com.gitee.wsl.compose.ui.table.model.DateColumnSpec
import com.gitee.wsl.compose.ui.table.model.DoubleColumnSpec
import com.gitee.wsl.compose.ui.table.model.DropdownColumnSpec
import com.gitee.wsl.compose.ui.table.model.IntColumnSpec
import com.gitee.wsl.compose.ui.table.Table
import com.gitee.wsl.compose.ui.table.model.TextColumnSpec
import com.gitee.wsl.compose.ui.table.model.WidthSetting
import kotlinx.datetime.LocalDate

val chartSampleView = object : SampleView {
    override val name: String = "Table View"

    override val thumbnail = @Composable {
        ContextTable()
    }

    override val content: @Composable () -> Unit = @Composable {
        ContextTable()
    }
}

@Composable
fun ContextTable(){
    val columnSpecs = listOf<ColumnSpec<Movie, *>>(
        TextColumnSpec("Title", WidthSetting.Flex(1f)) { it.title },
        DateColumnSpec("Release Date", WidthSetting.WrapContent, { it.releaseDate }),
        DoubleColumnSpec("Rating", WidthSetting.WrapContent, valueSelector = { it.rating }),
        IntColumnSpec("Awards", WidthSetting.WrapContent, { it.awardCount }),
        DropdownColumnSpec(
            "Genre",
            WidthSetting.WrapContent,
            { it.genre },
            { it.stringValue },
            Genre.entries.toList(),
            onChoicePicked = {}
        ),
        CheckboxColumnSpec("Watched", WidthSetting.WrapContent) { it.watched }
    )

    Column {
        var selectedCount by remember { mutableStateOf(0) }

        Table(
            columnSpecs,
            movies,
            modifier = Modifier.padding(20.dp),
            showSelectionColumn = true,
            onSelectionChange = { list -> selectedCount = list.size }
        )

        if (selectedCount > 0) {
            Text("Selected: $selectedCount")
        }

    }
}

private enum class Genre(val stringValue: String) {
    ACTION("Action"), DRAMA("Drama"), ADVENTURE("Adventure"),
    CRIME("Crime"), SCI_FI("Science Fiction")
}

private data class Movie(
    val title: String,
    val releaseDate: LocalDate,
    val rating: Double,
    val genre: Genre,
    val watched: Boolean,
    val awardCount: Int
)

private val movies = listOf(
    Movie(
        title = "Guardians of the API",
        releaseDate = LocalDate(2014, 8, 1),
        rating = 8.0,
        genre = Genre.ACTION,
        watched = true,
        awardCount = 52
    ),
    Movie(
        title = "Compose Club",
        releaseDate = LocalDate(1999, 10, 15),
        rating = 8.8,
        genre = Genre.DRAMA,
        watched = true,
        awardCount = 11
    ),
    Movie(
        title = "Jetpack to the Future",
        releaseDate = LocalDate(1985, 7, 3),
        rating = 8.5,
        genre = Genre.ADVENTURE,
        watched = true,
        awardCount = 19
    ),
    Movie(
        title = "Runtime Fiction",
        releaseDate = LocalDate(1994, 10, 14),
        rating = 8.9,
        genre = Genre.CRIME,
        watched = false,
        awardCount = 70
    ),
    Movie(
        title = "The Layout Matrix",
        releaseDate = LocalDate(1999, 3, 31),
        rating = 8.7,
        genre = Genre.SCI_FI,
        watched = true,
        awardCount = 46
    ),
    Movie(
        title = "Exception: Impossible",
        releaseDate = LocalDate(1996, 5, 22),
        rating = 7.1,
        genre = Genre.ACTION,
        watched = false,
        awardCount = 11
    )
)

